//
//  BGAddInfoTableViewCell.m
//  bigMall
//
//  Created by LTOVE on 16/8/29.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGAddInfoTableViewCell.h"
#import "BGAddinfopickViewModel.h"
#import "MJExtension.h"
#define LineMagin 10
static NSString *const reuseIdentify = @"cell";
@interface BGAddInfoTableViewCell ()<BGAddinfopickViewModelDelegate,UITextFieldDelegate>

@end

@implementation BGAddInfoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
/**
 *  pickView选择的结果
 *
 *  @param str str description
 */
- (void)pickViewValueChanged:(NSString *)str
{
    self.model.changedStr = str;
    
    NSMutableString *titleStr = [NSMutableString stringWithFormat:@"%@",self.model.title];
    NSRange rang = [titleStr rangeOfString:@":"];
    titleStr = [NSMutableString stringWithFormat:@"%@",[titleStr substringToIndex:rang.location + 1]];
    [titleStr appendString:[NSString stringWithFormat:@" %@",str]];
    self.model.title = titleStr;
//    GWLog(@"-------->%@",self.model.keyValues);
    [self addDeleGateWithModel:self.model];
}
/**
 *  textFildWen本改变
 *
 *  @param textField textField description
 */
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.model.changedStr = textField.text;
    self.model.textFildStr = textField.text;
    [self addDeleGateWithModel:self.model];
}

/**
 *  多选按钮触发
 *
 *  @param btn btn description
 */
- (void)btnClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    
    if ([self.model.doubBtnSelected[btn.tag] isEqualToString:@"0"]) {
        [self.model.doubBtnSelected replaceObjectAtIndex:btn.tag withObject:@"1"];
    }else{
        [self.model.doubBtnSelected replaceObjectAtIndex:btn.tag withObject:@"0"];
    }
    
//    self.model.doubBtnSelected = btn.selected;
    NSMutableArray *doubleValueArray ;
    BGAddInfoDoubleSelectedViewModel *itemModel = (BGAddInfoDoubleSelectedViewModel *)_model;
    if (!itemModel.changeDoubleArrayValue) {
        doubleValueArray = [NSMutableArray array];
    }else{
        doubleValueArray = [NSMutableArray arrayWithArray:itemModel.changeDoubleArrayValue];
    }
    if (btn.selected) {
        //选中状态
        if (doubleValueArray.count == 0) {
             [doubleValueArray addObject:itemModel.doubleSelectDateArray[btn.tag]];
        }else{
            NSString *tempStr = itemModel.doubleSelectDateArray[btn.tag];
            BOOL isYes = NO;
        for (NSString *str in doubleValueArray) {
            if ([str isEqualToString:tempStr]) {
//                return;
//                [doubleValueArray addObject:itemModel.doubleSelectDateArray[btn.tag]];
//                return;
                isYes = YES;
//                continue;
            }
            
         }
            if (!isYes) {
                [doubleValueArray addObject:itemModel.doubleSelectDateArray[btn.tag]];
            }
        }
    }else{
        //没有选中
        for (NSString *str  in doubleValueArray) {
            if ([str isEqualToString:itemModel.doubleSelectDateArray[btn.tag]]) {
//                [doubleValueArray removeObject:str];
            }
        }
    }
    self.model.changeDoubleArrayValue = doubleValueArray;
    [self addDeleGateWithModel:self.model];
    
}

- (void)addDeleGateWithModel:(BGAddInfoBasicModel *)model
{
    if ([_delegate respondsToSelector:@selector(cellValueChangedStr:)]) {
        [_delegate cellValueChangedStr:model];
    }
}

- (void)setModel:(BGAddInfoBasicModel *)model
{
    _model = model;
    //设置所有子空间
    [self setUpAllSubViewsWithModel:model];
    
}
/**
 *  根据模型添加所有子控件
 *
 *  @param model model description
 */
- (void)setUpAllSubViewsWithModel:(BGAddInfoBasicModel *)model
{
    //cell视图初始化
    self.textLabel.text = nil;
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    //加载控件
    if ([model isKindOfClass:[BGAddinfopickViewModel class]]) {
        //pickView下拉
        BGAddinfopickViewModel *itemModel = (BGAddinfopickViewModel *)model;
        itemModel.delegate = self;
        self.textLabel.text = model.title;
        self.textLabel.attributedText = [self attrStrWithStr:self.model.title withColor:textGrayColor];
    }else if ([model isKindOfClass:[BGAddinfoTextFildViewModel class]]){
        //文本填写视图
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 44)];
        UILabel *titleLabel = [self setUpLabelWithTitle:model.title];
        titleLabel.centerY = view.height * 0.5;
        UITextField *textFild = [UITextField new];
        textFild.delegate = self;
        textFild.placeholder = @"请填写";
        textFild.text = model.textFildStr;
        textFild.font = Font30;
        textFild.textColor = textGrayColor;
        [textFild sizeToFit];
        textFild.width = screenW - 2 * BGMaginLeft - titleLabel.width;
        textFild.x = CGRectGetMaxX(titleLabel.frame) + 5;
        textFild.centerY = titleLabel.centerY;
        [view addSubview:titleLabel];
        [view addSubview:textFild];
        [self.contentView addSubview:view];
    }else if ([model isKindOfClass:[BGAddInfoDoubleSelectedViewModel class]]){
        //多选
        BGAddInfoDoubleSelectedViewModel *itemModel = (BGAddInfoDoubleSelectedViewModel *)model;
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, itemModel.heigh)];
        //多选测试标题
        UILabel *titleLabel = [self setUpLabelWithTitle:itemModel.title];
        UIView *btnView = [self dynamicAddSectionWith:itemModel.doubleSelectDateArray];
        btnView.point = CGPointMake(BGMaginLeft, CGRectGetMaxY(titleLabel.frame));
        [view addSubview:titleLabel];
        [view addSubview:btnView];
        
//        view.userInteractionEnabled = YES;
        
        
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.size = CGSizeMake(100, 100);
//        btn.point = CGPointMake(0, 0);
//        [btn setTitle:@"sss" forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btn setBackgroundImage:[UIImage imageWithColor:read_ size:btn.size] forState:UIControlStateNormal];
//        [view addSubview:btn];
        [self.contentView addSubview:view];
        
        //动态添加选项
        
    }
}
- (NSMutableAttributedString *)attrStrWithStr:(NSString *)str withColor:(UIColor *)color
{
    NSDictionary *dic = @{
                          NSFontAttributeName:Font34,
                          NSForegroundColorAttributeName:textBlackColor
                          };
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:str attributes:dic];
    NSRange rang = [str rangeOfString:@":"];
    NSDictionary *secDic = @{
                             NSFontAttributeName:Font30,
                             NSForegroundColorAttributeName:color
                             };
    [attr addAttributes:secDic range:NSMakeRange(rang.location + 1, str.length -1 - rang.location)];
    return attr;
}
- (UIView *)dynamicAddSectionWith:(NSArray *)dateArray
{
    UIView *view = [[UIView alloc]init];
    view.size = CGSizeMake(screenW, 100);
    CGFloat btnHeigh = 0.0;
    
    for (int i = 0; i<dateArray.count; i ++) {
        NSString *titleStr = dateArray[i];
        //添加文本  因为需要文本的高度
        UILabel *label = [UILabel new];
        label.text = titleStr;
        label.font = Font30;
        label.textColor = textGrayColor;
        [label sizeToFit];
        //添加btn
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [btn setTitle:titleStr forState:UIControlStateNormal];
        btn.size = CGSizeMake(label.height, label.height);
//        [btn sizeToFit];
        [btn setBackgroundImage:[UIImage imageNamed:@"pay_unselect"] forState:UIControlStateNormal];
//        [btn setTitleColor:black_ forState:UIControlStateNormal];
        [btn setTitleColor:read_ forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"pay_selected"] forState:UIControlStateSelected];
//        CGColorSpaceRef
//        CGColorRef *cgColor = [CGColorCreate(;, <#const CGFloat * _Nullable components#>)]
//        btn.layer.borderWidth = 1.6;
//        if (btn.selected) {
//            btn.layer.borderColor = read_.CGColor;
//        }else{
//            btn.layer.borderColor = black_.CGColor;
//        }
        
        
        if ([self.model.doubBtnSelected[i] isEqualToString:@"0"]) {
            btn.selected = NO;
//            btn.layer.borderColor = black_.CGColor;
        }else{
            btn.selected = YES;
//            btn.layer.borderColor = read_.CGColor;
        };
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.x =   15;
        btn.y =   i * (btn.height + 5);
        label.x = CGRectGetMaxX(btn.frame) + 12;
        label.y = btn.y;
        btnHeigh = btn.height;
        btn.width = btn.width * 1.4;
        btn.layer.cornerRadius = 5;
        btn.layer.masksToBounds = YES;
        view.userInteractionEnabled = YES;
        btn.tag = i;
        [view addSubview:label];
        [view addSubview:btn];
        
    }
    CGFloat heigh = dateArray.count * (btnHeigh + 5);
    view.size = CGSizeMake(screenW, heigh);
    return view;
}


- (UILabel *)setUpLabelWithTitle:(NSString *)title
{
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.textColor = textBlackColor;
    titleLabel.font = Font34;
    [titleLabel sizeToFit];
    titleLabel.x = 16;
    titleLabel.y = LineMagin;
    return titleLabel;
}

+ (instancetype)cellWithTableview:(UITableView *)tableview
{
    BGAddInfoTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
@end
